from zope import schema
from zope.component import adapts, getMultiAdapter
from zope.formlib.form import FormFields

from zope.interface import implements
from zope.interface import Interface


from Products.CMFCore.utils import getToolByName
from Products.CMFDefault.formlib.schema import SchemaAdapterBase
from Products.CMFDefault.formlib.schema import ProxyFieldProperty
from Products.CMFPlone.interfaces import IPloneSiteRoot


from plone.app.controlpanel.form import ControlPanelForm

from my315ok.flowrate import flowrateMessageFactory as _

class IFlowRateSchema(Interface):
    """
    """
    
    sitename = schema.TextLine(
        title=_(u"site name"),
        description=_(u"provice a sitem name as flow access dic keyword."),
        required=True
        )


class SiteFlowControlPanelAdapter(SchemaAdapterBase):

    adapts(IPloneSiteRoot)
    implements(IFlowRateSchema)

    def __init__(self, context):
#        import pdb
#        pdb.set_trace()
        super(SiteFlowControlPanelAdapter, self).__init__(context)
        self.portal = context
        pprop = getToolByName(self.portal, 'portal_properties')
        self.context = pprop.site_properties

    def get_sitename(self):
        # uberselection widget does not like empty values
#        import pdb
#        pdb.set_trace()
        sitename = getattr(self.context, 'sitename', [])
        return sitename

    def set_sitename(self, value):
        if value is not None:
            self.context.sitename = value
        else:
            self.context.sitename = []
    sitename = property(get_sitename, set_sitename)

    
class SiteFlowControlPanel(ControlPanelForm):

    form_fields = FormFields(IFlowRateSchema)

    label = _("Site name")
    description = _("Setup Site name.")
    form_name = _("Flow Rate settings")

